<?php
class Rating_model extends MY_Model
{
	private $_table = 'rating';

	public function __construct()
	{
		parent::__construct();
	}

	public function sum_point($condition)
	{
		$this->db->select_sum('point');
			
		if($condition)
		{
			$this->db->where($condition);
		}

		$query = $this->db->get($this->_table);
		$point = $query->row()->point;

		return $point ? $point : 0;
	}

	public function avg_point($condition)
	{
		$this->db->select_avg('point');
			
		if($condition)
		{
			$this->db->where($condition);
		}

		$query = $this->db->get($this->_table);
		$point = $query->row()->point;

		return $point ? $point : 0;
	}

	public function is_rated($scene_id, $user_id = 0)
	{
		$condition = array(
			'scene_id' => $scene_id,
			'user_id' => $user_id,
			'ip_address' => $this->input->ip_address(),
			'flag' => 1,
		);

		return ($this->count($condition) > 0);
	}
}